package com.clip.util.mobile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Vector;



public class MobileHttpSender {
	
	
	public static String sendMobileMsg(String mobilenum,String msg){
		String url=MobileEnv.getMobileConfigByKey(MobileEnv.URL);
		Map parameters=new LinkedHashMap();
		HttpResponse httpResponse=null;
		try {
			parameters.put(MobileEnv.ENTID, MobileEnv.getMobileConfigByKey(MobileEnv.ENTID));
			parameters.put(MobileEnv.UID, MobileEnv.getMobileConfigByKey(MobileEnv.UID));
			parameters.put(MobileEnv.PWD, MobileEnv.getMobileConfigByKey(MobileEnv.PWD));
			parameters.put("mobs", mobilenum);
			parameters.put("msg", URLEncoder.encode(msg,"UTF-8"));
			parameters.put(MobileEnv.RETURNFLAG, MobileEnv.getMobileConfigByKey(MobileEnv.RETURNFLAG));
			httpResponse=send(url, MobileEnv.getMobileConfigByKey(MobileEnv.METHOD), parameters, null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return httpResponse.getContent();
	}
	public static HttpResponse send(String urlString, String method,   
            Map<String, String> parameters, Map<String, String> propertys)   
            throws IOException {   
        HttpURLConnection urlConnection = null;   
    
        if (method.equalsIgnoreCase("GET") && parameters != null) {   
            StringBuffer param = new StringBuffer();   
            int i = 0;   
            for (String key : parameters.keySet()) {   
                if (i == 0)   
                    param.append("?");   
                else  
                    param.append("&");   
                param.append(key).append("=").append(parameters.get(key));   
                i++;   
            }   
            urlString += param;   
        }   
        URL url = new URL(urlString);   
        urlConnection = (HttpURLConnection) url.openConnection();   
    
        urlConnection.setRequestMethod(method);   
        urlConnection.setDoOutput(true);   
        urlConnection.setDoInput(true);   
        urlConnection.setUseCaches(false);   
    
        if (propertys != null)   
            for (String key : propertys.keySet()) {   
                urlConnection.addRequestProperty(key, propertys.get(key));   
            }   
    
        if (method.equalsIgnoreCase("POST") && parameters != null) {   
            StringBuffer param = new StringBuffer();   
            for (String key : parameters.keySet()) {   
                param.append("&");   
                param.append(key).append("=").append(parameters.get(key));   
            }   
            urlConnection.getOutputStream().write(param.toString().getBytes());   
            urlConnection.getOutputStream().flush();   
            urlConnection.getOutputStream().close();   
        }   
    
        return makeContent(urlString, urlConnection);   
    }
	
	public static HttpResponse makeContent(String urlString,   
            HttpURLConnection urlConnection) throws IOException {   
		HttpResponse httpResponser = new HttpResponse();   
        try {   
            InputStream in = urlConnection.getInputStream();   
            BufferedReader bufferedReader = new BufferedReader(   
                    new InputStreamReader(in,"GBK"));   
            Vector<String> contentCollection=httpResponser.getContentCollection();
            contentCollection= new Vector<String>();   
            StringBuffer temp = new StringBuffer();   
            String line = bufferedReader.readLine();   
            while (line != null) {   
            	contentCollection.add(line);   
                temp.append(line).append("\r\n");   
                line = bufferedReader.readLine();   
            }   
            bufferedReader.close();   
            httpResponser.setContent(new String(temp.toString().getBytes()));   
    
            return httpResponser;   
        } catch (IOException e) {   
            throw e;   
        } finally {   
            if (urlConnection != null)   
                urlConnection.disconnect();   
        }   
    }
}
